//
//  AXFCreditsMallVC.m
//  BEEQUICK
//
//  Created by admin on 20/11/2016.
//  Copyright © 2016 SevenGroup. All rights reserved.
//

#import "AXFCreditsMallVC.h"
#import "AXFCredictsMallFlowLayout.h"
#import "AXFRecordAndRuleCell.h"
#import "AXFMoneyCouponCell.h"
#import "AXFCircleAniCell.h"
#import "AXFShowCouponVC.h"
#import "AXFConvertRecordVC.h"
#import "AXFCreditsRuleVC.h"
#import "AXFWhiteNavView.h"

@interface AXFCreditsMallVC () <UICollectionViewDataSource, UICollectionViewDelegate, AXFWhiteNavViewDelegate>

@property (strong, nonatomic) NSArray<NSDictionary *> *rrArr;
@property (strong, nonatomic) NSArray<NSDictionary *> *mcArr;

@end

@implementation AXFCreditsMallVC

static NSString * const circleID = @"circleID";
static NSString * const rrCellID = @"rrcellID";
static NSString * const mcCellID = @"mccellID";

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
	[self loadData];
}

- (void)loadData {
	NSArray *rrArr = @[@{@"imgName":@"icon_points_exchangrecord", @"labelTxt":@"兑换记录"},
					   @{@"imgName":@"icon_points_rule", @"labelTxt":@"积分规则"}
					   ];
	NSArray *mcArr = @[@{@"imgName":@"", @"moneyTxt":@"3 元积分兑换券", @"creditsTxt":@"300 积分"},
					   @{@"imgName":@"", @"moneyTxt":@"5 元积分兑换券", @"creditsTxt":@"500 积分"},
					   @{@"imgName":@"", @"moneyTxt":@"8 元积分兑换券", @"creditsTxt":@"800 积分"},
					   @{@"imgName":@"", @"moneyTxt":@"10 元积分兑换券", @"creditsTxt":@"1000 积分"}
					   ];
	
	_rrArr = rrArr;
	_mcArr = mcArr;
}

- (void)setupUI {
	self.navigationItem.title = @"积分商城";
	
//	self.navigationController.navigationBar.hidden = NO;
	
	self.view.backgroundColor = [UIColor whiteColor];
	
	AXFCredictsMallFlowLayout *layout = [[AXFCredictsMallFlowLayout alloc]init];
	UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 44, kSCREENWIDTH, kSCREENHEIGHT) collectionViewLayout:layout];
	
	[self.view addSubview:collectionView];
	
	collectionView.backgroundColor = [UIColor cz_colorWithHex:0xefefef];
	collectionView.scrollEnabled = YES;
	collectionView.dataSource = self;
	collectionView.delegate = self;
	
	[collectionView	registerClass:[AXFCircleAniCell class] forCellWithReuseIdentifier:circleID];
	[collectionView registerClass:[AXFRecordAndRuleCell class] forCellWithReuseIdentifier:rrCellID];
	[collectionView registerClass:[AXFMoneyCouponCell class] forCellWithReuseIdentifier:mcCellID];

	AXFWhiteNavView *whiteNavView = [[AXFWhiteNavView alloc] init];
	
	[whiteNavView setTitleName:@"积分商城" andFunctionName:nil];
	
	whiteNavView.delegate = self;
	
	[self.view addSubview:whiteNavView];
	
	[whiteNavView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.right.top.offset(0);
		make.height.mas_equalTo(kNAVBARH + 20);
	}];
}

- (void)backToViewController {
	[self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
	return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
	if (section == 0) {
		return 3;
	}
	return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
	if (indexPath.section == 0) {
		if (indexPath.item == 0) {
			AXFCircleAniCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:circleID forIndexPath:indexPath];
			return cell;
		}
		
		AXFRecordAndRuleCell *rrCell = [collectionView dequeueReusableCellWithReuseIdentifier:rrCellID forIndexPath:indexPath];
		[_rrArr enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
			if (indexPath.item == idx + 1) {
				rrCell.imgView.image = [UIImage imageNamed:obj[@"imgName"]];
				rrCell.label.text = obj[@"labelTxt"];
			}
		}];
		return rrCell;
	}

	AXFMoneyCouponCell *mcCell = [collectionView dequeueReusableCellWithReuseIdentifier:mcCellID forIndexPath:indexPath];
	[_mcArr enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
		if (indexPath.item == idx) {
			mcCell.imgView.image = [UIImage imageNamed:obj[@"imgName"]];
			mcCell.moneyLabel.text = obj[@"moneyTxt"];
			mcCell.creditsLabel.text = obj[@"creditsTxt"];
		}
	}];
	return mcCell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
	if (indexPath.section == 0) {
		if (indexPath.item == 0) {
			return CGSizeMake(kSCREENWIDTH, 183);
		} else {
			return CGSizeMake((kSCREENWIDTH - 1) / 2, 60);
		}
	}
	return CGSizeMake((kSCREENWIDTH - 1) / 2, 166);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
	if (indexPath.section == 0) {
		if (indexPath.item == 1) {
			[self setupVCAndPushWithVCName:@"AXFConvertRecordVC"];
		} else if (indexPath.item == 2) {
			[self setupVCAndPushWithVCName:@"AXFCreditsRuleVC"];
		}
	} else {
		NSString *urlStr = @"http://www.baidu.com";
		
		AXFShowCouponVC *showCouponVC = [[AXFShowCouponVC alloc] init];
		if (indexPath.item == 0) {
			urlStr = @"http://m.beequick.cn/show/pointDetail?id=1&__d=d14ryS1bFkB0dOBn%2Bvd9GDn55g34BaxSwQk6PAIdxaUcOZK4qZM5HpDIlnwAZPd5UyoGAl5VrxjngS7/VNfVPUj2iqrOmO6R3Ek0q/D53DOItoLlaw%2Bf/53dpER4x14eK17f&__v=ios5.9&location_hash=2e7577lNaa2SGYIfkVIoNiFqGKZJN%2B5FTxs8%2By9kiloWjXw%2BgbQ72jrK4GAqINz9FzBwcg1jo0ouZxrlweIQ&zchtauth=e2ce4bEoDMWWB15V98ApGBX150AVjnGXVN7pv7sV4WbS9fEQ&timestamp=1479966721254331&token=222f92ef2ba0b2f15c6f38c4dcbc47cc&mac=26d887489d1c975779e1c207c7966d66";
		} else if (indexPath.item == 1) {
			urlStr = @"http://m.beequick.cn/show/pointDetail?id=2&__d=d14ryS1bFkB0dOBn%2Bvd9GDn55g34BaxSwQk6PAIdxaUcOZK4qZM5HpDIlnwAZPd5UyoGAl5VrxjngS7/VNfVPUj2iqrOmO6R3Ek0q/D53DOItoLlaw%2Bf/53dpER4x14eK17f&__v=ios5.9&location_hash=2e7577lNaa2SGYIfkVIoNiFqGKZJN%2B5FTxs8%2By9kiloWjXw%2BgbQ72jrK4GAqINz9FzBwcg1jo0ouZxrlweIQ&zchtauth=e2ce4bEoDMWWB15V98ApGBX150AVjnGXVN7pv7sV4WbS9fEQ&timestamp=1479966855829664&token=222f92ef2ba0b2f15c6f38c4dcbc47cc&mac=d154753c39c89da00af76ef65971758d";
		} else if (indexPath.item == 2) {
			urlStr = @"http://m.beequick.cn/show/pointDetail?id=3&__d=d14ryS1bFkB0dOBn%2Bvd9GDn55g34BaxSwQk6PAIdxaUcOZK4qZM5HpDIlnwAZPd5UyoGAl5VrxjngS7/VNfVPUj2iqrOmO6R3Ek0q/D53DOItoLlaw%2Bf/53dpER4x14eK17f&__v=ios5.9&location_hash=2e7577lNaa2SGYIfkVIoNiFqGKZJN%2B5FTxs8%2By9kiloWjXw%2BgbQ72jrK4GAqINz9FzBwcg1jo0ouZxrlweIQ&zchtauth=e2ce4bEoDMWWB15V98ApGBX150AVjnGXVN7pv7sV4WbS9fEQ&timestamp=1479966892680107&token=222f92ef2ba0b2f15c6f38c4dcbc47cc&mac=8bbba2e34c19231fbdde549da9782956";
		} else {
			urlStr = @"http://m.beequick.cn/show/pointDetail?id=4&__d=d14ryS1bFkB0dOBn%2Bvd9GDn55g34BaxSwQk6PAIdxaUcOZK4qZM5HpDIlnwAZPd5UyoGAl5VrxjngS7/VNfVPUj2iqrOmO6R3Ek0q/D53DOItoLlaw%2Bf/53dpER4x14eK17f&__v=ios5.9&location_hash=2e7577lNaa2SGYIfkVIoNiFqGKZJN%2B5FTxs8%2By9kiloWjXw%2BgbQ72jrK4GAqINz9FzBwcg1jo0ouZxrlweIQ&zchtauth=e2ce4bEoDMWWB15V98ApGBX150AVjnGXVN7pv7sV4WbS9fEQ&timestamp=1479966914122554&token=222f92ef2ba0b2f15c6f38c4dcbc47cc&mac=92750516008cc2621fdb4e69a35497f5";
		}
		showCouponVC.urlStr = urlStr;
		
		[self.navigationController pushViewController:showCouponVC animated:YES];
	}
}

- (void)setupVCAndPushWithVCName:(NSString *)vcName {
	Class cls = NSClassFromString(vcName);
	UIViewController *vc = [[cls alloc] init];
	[self.navigationController pushViewController:vc animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
